import com.troyworks.framework.ui.BaseComponent;
import com.troyworks.hsmf.AEvent;
import com.troyworks.framework.ui.FontManager;
import com.troyworks.framework.ui.ToolTip;
import com.troyworks.events.EVTD;
import flash.filters.GlowFilter;
import mx.managers.FocusManager;

/**
 * @author Troy Gardner
 */
class com.troyworks.framework.ui.BaseForm extends BaseComponent {
	public var minTabIndex:Number;
	public var maxTabIndex:Number;
	public var requiredFieldAsteriskColor:Number = 0xff0066;
	private var tooltip : ToolTip;
	private var errorGlow : GlowFilter;
	public var focusMan : FocusManager;
	private var filtersArr : Array = new Array();

	private var lastErrorField : MovieClip = null;

	private var currentField : Object;
	private var errorIndicateIntV : Number;
	private var modelValid : Boolean;
	public static var  tIdx : Number = 0;
	
	function BaseForm(initialState : Function, hsmfName : String, aInit : Boolean) {
		super(initialState, hsmfName, aInit);
		focusMan = new FocusManager();
	}
	/***********************************
	 * For any clip with Lbl in it's name
	 * disable it from tabbing.
	 */
	public function disableTabOnLabels():Void{
		for(var i:String in this){
			
			var mc:Object = Object(this[i]);
		//	trace("disabling tab on  " + mc._name);
			if(String(mc._name).indexOf("Lbl") > -1){
			
				MovieClip(mc).tabEnabled =false;
			//	MovieClip(mc)._visible =false;
				if(mc instanceof TextField){
					TextField(mc).selectable =false;
				}
				
			}
		}
	}
	/***********************************
	 * Using the FontManager, who has loaded
	 * up fonts dynamically
	 * get the appropriate style
	 */
	public function styleTextFields() : Void{
		for (var i : String in this) {
		//	trace(i + " " + this[i]);
			var o:Object = this[i];
			if(o instanceof TextField){
				var tf:TextField = TextField(o);
				//trace("FOund a TextField");
				FontManager.getInstance().styleMe(this,tf);
			}
			
		}
	}
	public function setTextFieldsToHTML():Void{
		for (var i : String in this) {
		//	trace(i + " " + this[i]);
			var o:Object = this[i];
			if(o instanceof TextField){
				var tf:TextField = TextField(o);
				tf.html = true;
			}
			
		}	
	}
	/***************************************
	 * For a given string e.g. " this * means required"
	 * convert it into the 
	 */
	public function styleAsterisk(str:String, colorOverride:Number):String{
		var color:Number = (colorOverride == null)? requiredFieldAsteriskColor : colorOverride;
		var res:String = null;
		var a:Number = 0;
		var b:Number = str.indexOf("*");
		var c:Number = str.length;
		var AA:String = str.substring(a, b);
		var BB:String = "<font color='#"+color.toString(16)+"'>*</font>";
		var CC:String = str.substring(b+1,c);
		res = AA + BB + CC;
		return res;
	}
	function setTabIndexes(from:Number):Void{
	
	}
	public static function passBlankStringIfNull(field:Object):String{
		var res:String = "";
		if(field != null){
			res = String( field);
		}
		return res;
	}
	public function onFieldFocusIn(evt:EVTD):Void{
	trace("HIGHLIGHTO onFieldFocusIn " + MovieClip(evt.target)._name);	
		if(evt.target == lastErrorField){
			//going back to correct
			clearInterval(errorIndicateIntV);
		}
	}
	public function activateErrorUI():Void{
		tooltip._visible =true;
		lastErrorField.filters = filtersArr;
		clearInterval(errorIndicateIntV);
		//lastErrorField = null;
	}
	public function startErrorCountdown():Void{
		   errorIndicateIntV = setInterval(this, "activateErrorUI", 250);
	}
	/* called after a field has lost focus, indicating the user is finished
	 * and if there is an error with the field it should be popped up.
	 */
	public function onFieldFocusOut(evt : EVTD) : Void{
	
		trace("HIGHLIGHTB onFieldFocusOut " + MovieClip(evt.target)._name  + " " + lastErrorField);
		///set model
		tooltip._visible = false;
		//perform validation//
		//
		if(lastErrorField == null){
			onFieldDataChanged(evt);
		}
		if(lastErrorField != null){
			startErrorCountdown();
		}
	}
	public function onFieldDataChanged(evt:EVTD):Void{
				clearFormError(MovieClip(evt.target));
		currentField = evt.target;
		///set model
		tooltip._visible = false;
	}
	public function clearFormError(_mc : MovieClip) : Void{
			tooltip._visible = false;
			_mc.filters = [];
			lastErrorField = null;
	}
	    // A very simple custom validation method.
     private function validateModel():Void
      {
                modelValid =  true;
   
      }
	/*.................................................................*/
	function s0_viewAssetsLoaded(e : AEvent) : Function
	{
		this.onFunctionEnter ("s0_viewAssetsLoaded-", e, []);
		switch (e.sig)
		{
			case Q_ENTRY_SIG :
			{
				tooltip = ToolTip(attachMovie("ValidationErrorBubble","tooltip_mc",getNextHighestDepth()));
				tooltip._visible = false;
				disableTabOnLabels();
				styleTextFields();
				return null;
			}
		}
		return super.s0_viewAssetsLoaded(e);
	}
}